'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'Hello Word021';
  }

  /**
   * EJS 模板引擎
   */
  async home() {
    const { ctx, app } = this;

    /**
     * @param ctx.rander
     * 第一个参数 要展示的html文件名
     * 第二个参数 需要的数据
     */
    console.log(ctx.session.counter);
    await ctx.render('app.html', {
      nowTime: app.timeProp,
      id: 2021,
      name: '小红',
      age: 18,
      aihao: [
        '唱',
        '跳',
        'rap',
      ],
    });
  }

  async add() {
    const { ctx } = this;
    ctx.cookies.set('user', {
      name: 'woooc',
    });
    ctx.session.username = 'sad';

    ctx.body = {
      code: 200,
      msg: 'success',
      data: 'cookie add success',
    };
  }
  async del() {
    const { ctx } = this;
    ctx.cookies.set('user', null);

    ctx.body = {
      code: 200,
      msg: 'success',
      data: 'cookie del success',
    };

  }
  async edit() {
    const { ctx } = this;
    ctx.cookies.set('user', 'Cookie');

    ctx.body = {
      code: 200,
      msg: 'success',
      data: 'cookie edit success',
    };
  }
  async view() {
    const { ctx } = this;
    const cookieData = ctx.cookies.get('user');
    ctx.body = {
      code: 200,
      msg: 'success',
      data: cookieData,
    };
  }
}

module.exports = HomeController;
